home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / Bob / Debug.cp < prev    next >
Encoding:
Text File  |  1995-12-12  |  2.7 KB  |  142 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    Debug.cp - debug routines
  4.   *
  5.   *    Original code: Copyright (c) 1991, by David Michael Betz.  All rights reserved
  6.   *    Modifications and additions: Copyright © by Christopher E. Hyde, 1995
  7.   *
  8.   ***/
  9.  
  10. #include "Bob.h"
  11.  
  12.  
  13. // instruction output formats
  14. enum {     kFmtNone, kFmtByte, kFmtWord, kFmtLit };
  15.  
  16. struct TOpDef {
  17.     char    fName[5];
  18.     uchar    fOpCode:6;
  19.     uchar    fFormat:2;
  20. };
  21.  
  22. static TOpDef pOpTab[] = {
  23. #define    Op(n, f)    { #n, op##n, kFmt##f },
  24.     Op(BRT,        Word)
  25.     Op(BRF,        Word)
  26.     Op(BR,        Word)
  27.     Op(LIT,        Lit)
  28.     Op(REF,        Lit)
  29.     Op(SET,        Lit)
  30.     Op(AREF,    Byte)
  31.     Op(ASET,    Byte)
  32.     Op(TREF,    Byte)
  33.     Op(TSET,    Byte)
  34.     Op(MREF,    Byte)
  35.     Op(MSET,    Byte)
  36.     Op(VREF,    None)
  37.     Op(VSET,    None)
  38.     Op(CALL,    Byte)
  39.     Op(RTS,        None)
  40.     Op(SEND,    Byte)
  41.     Op(TSPC,    Byte)
  42.     Op(NIL,        None)
  43.     Op(PUSH,    None)
  44.     Op(NOT,        None)
  45.     Op(NEG,        None)
  46.     Op(ADD,        None)
  47.     Op(SUB,        None)
  48.     Op(MUL,        None)
  49.     Op(DIV,        None)
  50.     Op(REM,        None)
  51.     Op(SHL,        None)
  52.     Op(SHR,        None)
  53.     Op(BAND,    None)
  54.     Op(BOR,        None)
  55.     Op(XOR,        None)
  56.     Op(BNOT,    None)
  57.     Op(LT,        None)
  58.     Op(LE,        None)
  59.     Op(EQ,        None)
  60.     Op(NE,        None)
  61.     Op(GE,        None)
  62.     Op(GT,        None)
  63.     Op(INC,        None)
  64.     Op(DEC,        None)
  65.     Op(DUP2,    None)
  66.     Op(NEW,        None)
  67.     Op(INT,        Word)
  68.     {0, 0, 0}
  69. };
  70.  
  71.  
  72. static void
  73. NewLine (void)
  74. {
  75.     gOutput.NewLine();
  76. }
  77.  
  78.  
  79. // Dump the instructions in a code object to stderr
  80. void
  81. DumpProcedure (ConstValue code)
  82. {
  83.     const TVector* codeV = code->fVec;
  84.     int len = SLen(&codeV->fData[kIByteCodes]);
  85.  
  86.     for (int lc = 0; lc < len; )
  87.         lc += DumpInstruction(codeV, lc);
  88.     NewLine();
  89. }
  90.  
  91.  
  92. // Dump a single bytecode instruction to stderr
  93. int
  94. DumpInstruction (const TVector* code, int lc)
  95. {
  96.     ConstValue vec = code->fData;
  97.     if (lc == 0) {        // Show the function name
  98.         NewLine();
  99.         if (vec[kIClass].fType == tClass) {        // Show the class name
  100.             TId name;
  101.             GetCString(name, sizeof(name), clgetname(&vec[kIClass]));
  102.             PrintErrF("%s::", name);
  103.         }
  104.         Print(&stderr_iostream, false, &vec[kIName]);
  105.         NewLine();
  106.     }
  107.  
  108.         // Get bytecode pointer for this instruction
  109.     CodePtr cp = (CodePtr) vec[kIByteCodes].fStr->fData + lc;
  110.  
  111.         // Show the address and opcode
  112.     uchar opCode = *cp++;
  113.     PrintErrF("%04X:  %02X ", lc, opCode);
  114.  
  115.         // Display the operands
  116.     for (TOpDef* op = pOpTab; op->fName[0]; ++op)
  117.         if (opCode == op->fOpCode) {
  118.             switch (op->fFormat) {
  119.             case kFmtNone:
  120.                 PrintErrF("      \t%s\r", op->fName);
  121.                 return 1;
  122.             case kFmtByte:
  123.                 PrintErrF("%02X    \t%s %02X\r", *cp, op->fName, *cp);
  124.                 return 2;
  125.             case kFmtWord:
  126.                 PrintErrF("%02X %02X \t%s %02X%02X\r",
  127.                                     *cp, cp[1], op->fName, *cp, cp[1]);
  128.                 return 3;
  129. //            case kFmtLit:
  130.             default:
  131.                 PrintErrF("%02X    \t%s %02X ; ", *cp, op->fName, *cp);
  132.                 Print(&stderr_iostream, true, &vec[*cp]);
  133.                 NewLine();
  134.                 return 2;
  135.             }
  136.         }
  137.  
  138.     // Unknown opcode
  139.     PrintErrF("      <UNKNOWN>\r");
  140.     return 1;
  141. }
  142.